package com.ruoyi.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.SurgeryDevice;
import com.ruoyi.manage.domain.SurgeryDeviceDetail;
import com.ruoyi.manage.dto.SurgeryDeviceDto;
import com.ruoyi.manage.mapper.SurgeryDeviceMapper;
import com.ruoyi.manage.service.ISurgeryDeviceDetailService;
import com.ruoyi.manage.service.ISurgeryDeviceService;
import com.ruoyi.manage.vo.SurgeryDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.bean.BeanUtil;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 术中清点记录最外层 记录第几次时间等Service业务层处理
 *
 * @author zww
 * @date 2025-07-08
 */
@Service
public class SurgeryDeviceServiceImpl implements ISurgeryDeviceService {
    @Autowired
    private SurgeryDeviceMapper surgeryDeviceMapper;
    @Autowired
    private ISurgeryDeviceDetailService surgeryDeviceDetailService;
    @Autowired
    private EventSurgeryServiceImpl eventSurgeryServiceImpl;

    /**
     * 查询术中清点记录最外层 记录第几次时间等
     *
     * @param surgeryDeviceId 术中清点记录最外层 记录第几次时间等主键
     * @return 术中清点记录最外层 记录第几次时间等
     */
    @Override
    public SurgeryDevice selectSurgeryDeviceBySurgeryDeviceId(Long surgeryDeviceId) {
        if (surgeryDeviceId == null) {
            return null;
        }
        SurgeryDevice surgeryDevice = surgeryDeviceMapper.selectSurgeryDeviceBySurgeryDeviceId(surgeryDeviceId);
        if (surgeryDevice != null) {
            loadSurgeryDeviceDetails(surgeryDevice);
        }
        return surgeryDevice;
    }

    /**
     * 查询术中清点记录最外层 记录第几次时间等列表
     *
     * @param surgeryDevice 术中清点记录最外层 记录第几次时间等
     * @return 术中清点记录最外层 记录第几次时间等
     */
    @Override
    public List<SurgeryDeviceVo> selectSurgeryDeviceList(SurgeryDevice surgeryDevice) {
        if (surgeryDevice == null) {
            return new ArrayList<>();
        }
        List<SurgeryDevice> surgeryDeviceList = surgeryDeviceMapper.selectSurgeryDeviceList(surgeryDevice);
        List<SurgeryDeviceVo> surgeryDeviceVoList = new ArrayList<>(surgeryDeviceList.size());

        for (SurgeryDevice device : surgeryDeviceList) {
            SurgeryDeviceVo vo = convertToVo(device);
            surgeryDeviceVoList.add(vo);
        }
        return surgeryDeviceVoList;
    }

    /**
     * 新增术中清点记录最外层 记录第几次时间等
     *
     * @param surgeryDevice 术中清点记录最外层 记录第几次时间等
     * @return 结果
     */
    @Transactional
    @Override
    public SurgeryDevice insertSurgeryDevice(SurgeryDevice surgeryDevice) {
        if (surgeryDevice == null) {
            return null;
        }
        setCreateInfo(surgeryDevice);
        if (surgeryDevice.getSurgeryDeviceId() == null) {
            surgeryDeviceMapper.insertSurgeryDevice(surgeryDevice);
        } else {
            updateSurgeryDevice(surgeryDevice);
        }
        insertSurgeryDeviceDetail(surgeryDevice);
        return surgeryDevice;
    }

    /**
     * 修改术中清点记录最外层 记录第几次时间等
     *
     * @param surgeryDevice 术中清点记录最外层 记录第几次时间等
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSurgeryDevice(SurgeryDevice surgeryDevice) {
        if (surgeryDevice == null) {
            return 0;
        }
        setUpdateInfo(surgeryDevice);
        surgeryDeviceMapper.deleteSurgeryDeviceDetailBySurgeryDeviceId(surgeryDevice.getSurgeryDeviceId());
        insertSurgeryDeviceDetail(surgeryDevice);
        return surgeryDeviceMapper.updateSurgeryDevice(surgeryDevice);
    }

    /**
     * 批量删除术中清点记录最外层 记录第几次时间等
     *
     * @param surgeryDeviceIds 需要删除的术中清点记录最外层 记录第几次时间等主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSurgeryDeviceBySurgeryDeviceIds(Long[] surgeryDeviceIds) {
        if (surgeryDeviceIds == null || surgeryDeviceIds.length == 0) {
            return 0;
        }
        surgeryDeviceDetailService.deleteSurgeryDeviceDetailBySurgeryDeviceDetailIds(surgeryDeviceIds);
        return surgeryDeviceMapper.deleteSurgeryDeviceBySurgeryDeviceIds(surgeryDeviceIds);
    }

    /**
     * 删除术中清点记录最外层 记录第几次时间等信息
     *
     * @param surgeryDeviceId 术中清点记录最外层 记录第几次时间等主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSurgeryDeviceBySurgeryDeviceId(Long surgeryDeviceId) {
        if (surgeryDeviceId == null) {
            return 0;
        }
        surgeryDeviceMapper.deleteSurgeryDeviceDetailBySurgeryDeviceId(surgeryDeviceId);
        return surgeryDeviceMapper.deleteSurgeryDeviceBySurgeryDeviceId(surgeryDeviceId);
    }

    @Transactional
    @Override
    public int addEvent(SurgeryDeviceDto surgeryDeviceDto) {
        int row = 0;
        SurgeryDevice surgeryDevice = BeanUtil.copyProperties(surgeryDeviceDto, SurgeryDevice.class);
        if (surgeryDeviceDto.getBeforeoperationEvent() != null) {
            Long id = eventSurgeryServiceImpl.insertEventSurgery(surgeryDeviceDto.getBeforeoperationEvent());
            surgeryDevice.setBeforeoperationId(id);
        }
        if (surgeryDeviceDto.getPreantralEvent() != null) {
            Long id = eventSurgeryServiceImpl.insertEventSurgery(surgeryDeviceDto.getPreantralEvent());
            surgeryDevice.setPreantralId(id);
        }
        if (surgeryDeviceDto.getAftersewingskinEvent() != null) {
            Long id = eventSurgeryServiceImpl.insertEventSurgery(surgeryDeviceDto.getAftersewingskinEvent());
            surgeryDevice.setAftersewingskinId(id);
        }
        if (surgeryDeviceDto.getCavityEvent() != null) {
            Long id = eventSurgeryServiceImpl.insertEventSurgery(surgeryDeviceDto.getCavityEvent());
            surgeryDevice.setCavityId(id);
        }
        return updateSurgeryDevice(surgeryDevice);
    }

    /**
     * 新增术中清点记录信息
     *
     * @param surgeryDevice 术中清点记录最外层 记录第几次时间等对象
     */
    public void insertSurgeryDeviceDetail(SurgeryDevice surgeryDevice) {
        if (surgeryDevice == null) {
            return;
        }
        List<SurgeryDeviceDetail> details = surgeryDevice.getSurgeryDeviceDetailList();
        if (StringUtils.isNotEmpty(details)) {
            Long deviceId = surgeryDevice.getSurgeryDeviceId();
            for (SurgeryDeviceDetail detail : details) {
                detail.setSurgeryDeviceId(deviceId);
                detail.setCreateBy(getUsername());
                surgeryDeviceDetailService.insertSurgeryDeviceDetail(detail);
            }
        }
    }

    /**
     * 加载手术设备明细信息
     *
     * @param surgeryDevice 手术设备对象
     */
    private void loadSurgeryDeviceDetails(SurgeryDevice surgeryDevice) {
        List<SurgeryDeviceDetail> details = surgeryDeviceMapper.selectSurgeryDeviceDetailList(surgeryDevice.getSurgeryDeviceId());
        surgeryDevice.setSurgeryDeviceDetailList(details);
    }

    /**
     * 将 SurgeryDevice 转换为 SurgeryDeviceVo
     *
     * @param device SurgeryDevice 对象
     * @return SurgeryDeviceVo 对象
     */
    private SurgeryDeviceVo convertToVo(SurgeryDevice device) {
        SurgeryDeviceVo vo = new SurgeryDeviceVo();
        BeanUtil.copyProperties(device, vo);
        List<SurgeryDeviceDetail> details = surgeryDeviceDetailService.selectSurgeryDeviceDetailList(vo.getSurgeryDeviceId());
        vo.setSurgeryDeviceDetailList(details);
        vo.setBeforeoperationEvent(eventSurgeryServiceImpl.selectEventSurgeryByEventSurgeryId(vo.getBeforeoperationId()));
        vo.setPreantralEvent(eventSurgeryServiceImpl.selectEventSurgeryByEventSurgeryId(vo.getPreantralId()));
        vo.setCavityEvent(eventSurgeryServiceImpl.selectEventSurgeryByEventSurgeryId(vo.getCavityId()));
        vo.setAftersewingskinEvent(eventSurgeryServiceImpl.selectEventSurgeryByEventSurgeryId(vo.getAftersewingskinId()));
        return vo;
    }

    /**
     * 设置创建信息
     *
     * @param surgeryDevice 手术设备对象
     */
    private void setCreateInfo(SurgeryDevice surgeryDevice) {
        surgeryDevice.setUpdateBy(getUsername());
        surgeryDevice.setCreateBy(getUsername());
    }

    /**
     * 设置更新信息
     *
     * @param surgeryDevice 手术设备对象
     */
    private void setUpdateInfo(SurgeryDevice surgeryDevice) {
        surgeryDevice.setUpdateBy(getUsername());
    }
}

